﻿
/*################################################################ AUTHOR - SAVITHRI MANNAZHIMANA BABU ################################################################ */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntityObjects;
using BAL.Controller;

namespace PresentationLayer.View.DepartmentRepresentative
{
    public partial class AssignStationeryItems : System.Web.UI.Page
    {
        List<Int32> currentRequestedQty_list;
        List<Int32> outstandingQty_list;
        AssignStationeryItemsControl assignItemsControl;
        Employee employee;
        Department dept;
        RequisitionDetails requisitionDetails;
        string depId, itemId, temp_req_id,temp_received_qty,empId;
        int reqformId, receivedQty,totalItems = 0, totalAssignQty = 0;      
        List<Employee> emp_list;
        bool flag = true;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Employee currentuser = (Employee) Session["CurrentUser"];
                empId=currentuser.EmpId;
                //empId="10040";                
                lblName.Text = (string)Session["itemName"];
                lblReceivedQty.Text = (string)Session["receivedQty"];
                itemId = (string)Session["itemId"];
                Label1.Visible = true;
                Label2.Visible = true;
                btnSubmit.Visible = true;
                lblName.Visible = true;
                lblReceivedQty.Visible = true;
                currentRequestedQty_list = new List<Int32>();
                outstandingQty_list = new List<Int32>();
                assignItemsControl = new AssignStationeryItemsControl();
                employee = new Employee();
                dept = new Department();

                dept = assignItemsControl.getDepartmentByRepId(empId);
                depId = dept.DeptId.ToString();
                emp_list = new List<Employee>();
                emp_list = assignItemsControl.getRequestedEmployee(itemId,depId);
                List<Retrival> retrival_list = new List<Retrival>();
                if (emp_list != null)
                {
                    btnSubmit.Visible = true;
                    lblError.Visible = false;
                    foreach (Employee emp in emp_list)
                    {
                        Retrival r = new Retrival();
                        int requestedQty = assignItemsControl.getCurrentWeekRequestedQtyToAssignToEmp(emp.EmpId, itemId);
                        r.RequestedQty = requestedQty;
                        r.OutstandingQty = assignItemsControl.getOutstandingQtyToAssignToEmp(emp.EmpId, itemId);
                        r.EmpName = emp.EmpName;
                        r.ReqFormId = emp.ReqFormId;
                        r.EmpId = emp.EmpId;
                        RequisitionDetails rd = assignItemsControl.getRequisitionDetails(emp.ReqFormId, itemId);
                        r.ReceivedQty = rd.ReceivedQty;
                        retrival_list.Add(r);
                        gvAssignStationeryItems.DataSource = retrival_list;
                        gvAssignStationeryItems.DataBind();
                        gvAssignStationeryItems.Rows[0].Cells[3].Focus();
                        ViewState["AssignedListView"] = retrival_list;
                    }
                }
                else
                {
                    Label1.Visible = false;
                    Label2.Visible = false;
                    lblName.Visible = false;
                    lblReceivedQty.Visible = false;
                    btnBack.Visible = true;
                    btnSubmit.Visible = false;                    
                    lblError.Visible=true;
                }

            }
        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            itemId = (string)Session["itemId"];
            totalItems = Convert.ToInt32(Session["receivedQty"]);
            assignItemsControl = new AssignStationeryItemsControl();
            lblError.Visible = false;

            foreach (GridViewRow row in gvAssignStationeryItems.Rows)
            {
                String txt = ((TextBox)row.FindControl("tbQty")).Text.ToString();

                int ReqQty = Convert.ToInt32(row.Cells[2].Text);

                if (txt != null && !txt.Equals(""))
                {
                    if (Convert.ToInt32(txt) > ReqQty)
                    {
                        Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('Assigned Quantity cannot be greater than Requested Qty');</script>");
                        ((TextBox)row.FindControl("tbQty")).Text = "";
                        flag = false;
                    }
                    else
                    {
                        totalAssignQty += Convert.ToInt32(txt);
                    }
                }

            }

            if (totalAssignQty > totalItems || totalAssignQty==0 || flag == false) 
            {

                Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('You should assign items to employees or Cannot assign more than received quantity ');</script>");
            }
            else
            {
                  lblError.Visible = false;
                  foreach (GridViewRow gvr in gvAssignStationeryItems.Rows)
                    {
                        requisitionDetails = new RequisitionDetails();
                        temp_received_qty = ((TextBox)gvr.FindControl("tbQty")).Text;
                        if (!(temp_received_qty.Equals("")))
                        {
                            receivedQty = Convert.ToInt32(temp_received_qty);
                        }
                        else
                        {
                            receivedQty = 0;
                        }
                        
                        temp_req_id = ((HiddenField)gvr.FindControl("hdnReqFormId")).Value;
                        reqformId = Convert.ToInt32(temp_req_id);
                        requisitionDetails.ReceivedQty = receivedQty;                        

                        //update receivedQty
                        assignItemsControl.updateRequisitionDetails(requisitionDetails, reqformId, itemId);
                    }
              

                   //Save count of assign item
                  int assignCount = (int)Session["AssignCount"];
                  ++assignCount;
                  Session["AssignCount"] = assignCount;

                  Response.Redirect("ViewDisbursementList.aspx");
                }
                
            }

        
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("ViewDisbursementList.aspx");
        }

        protected void gvAssignStationeryItems_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAssignStationeryItems.PageIndex = e.NewPageIndex;
            gvAssignStationeryItems.DataSource = ViewState["AssignedListView"];
            gvAssignStationeryItems.DataBind();
        }

        protected void gvAssignStationeryItems_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("ViewDisbursementList.aspx");
        }

    }
}